package com.sdjzu.wangfuying.disableautobrightness.service;

import com.sdjzu.wangfuying.disableautobrightness.bean.internal.Event;
import com.sdjzu.wangfuying.disableautobrightness.bean.remote.Package;

import java.util.LinkedList;
import java.util.List;

public class LogServiceIsNotAService {
    // init instance
    private static final LogServiceIsNotAService _service = new LogServiceIsNotAService();

    private static final int MAX_EVENT_NUM = 200;

    private List<Event> eventList = new LinkedList<>();

    public static LogServiceIsNotAService getInstance() {
        return _service;
    }

    public void add(String packageName, String comment) {
        Package pkg = new Package();
        pkg.setPackageName(packageName);
        add(pkg, comment);
    }

    public void add(Package pkg, String comment) {
        Event event = new Event();
        event.setTimestamp(System.currentTimeMillis());
        event.setPackage(pkg);
        event.setComment(comment);
        eventList.add(event);
        if (MAX_EVENT_NUM < eventList.size()) {
            synchronized (this) {
                int cutDownNum = eventList.size() - MAX_EVENT_NUM;
                while (cutDownNum-- > 0) {
                    eventList.remove(0);
                }
            }
        }
    }

    public List<Event> getEventList() {
        return eventList;
    }
}
